<?php
class ManagerAction extends Action {
		/**2014-4-9 吕春城
	 *首页显示修改数据
	 * */


	public function index() {
		
		$islogin=session("isLogin");
		if(!$islogin){
			$this->error("您还没有登录","index.php?act=Login");
		}

		$this->setContent();
		$this->display ();
	}
	/**2014-4-9 吕春城
	 * 提取内容和时间设置到前台模板
	 * */
	private function setContent() {
		
	$sql="select votetime_start as start,votetime_end as end ,message_content as content,limit_times from vote_message where message_ID=1 ";
	$result= M()->query($sql);
	$this->assign('start_time',$result[0]['start']);
	$this->assign('end_time',$result[0]['end']);
	$this->assign("limit_time",$result[0]['limit_times']);
	$this->assign('content',$result[0]['content']);
	
	$sql='SELECT class_name,class_ID  from vote_class';
	$result=M()->query($sql);
	$this->assign('class_name',$result);
	$sql='SELECT class_name,item_ID ,vote_item.item_name from vote_class,vote_item where vote_class.class_ID=vote_item.class_ID';
	$result=M()->query($sql);
	$this->assign('item_list',$result);
	
	$sql='SELECT class_name,score_name,score_ID from vote_class,vote_score where vote_class.class_ID=vote_score.class_ID';
	$result=M()->query($sql);
	$this->assign('score_list',$result);
	}
	
	public function setTime() {
        $islogin=session("isLogin");
        if(!$islogin){
            $this->error("您还没有登录","index.php?act=Login");
        }

		$time_start = I ( 'time_start' );
		$time_end = I ( 'time_end' );
		$sql = sprintf ( "update vote_message set votetime_start='%s' , votetime_end='%s' where message_ID=1 ", $time_start, $time_end );
		
		$result = M ()->execute ( $sql );
		if ($result) {
			return $this->ajaxReturn (true);
		} else {
			return $this->ajaxReturn (false);
		}
	}
	/**2014-4-27 吕春城 设置限制次数*/
	public function  setLimit()
	{
        $islogin=session("isLogin");
        if(!$islogin){
            $this->error("您还没有登录","index.php?act=Login");
        }

		$count=I('limit');
		$sql=sprintf("update vote_message set limit_times=%s where message_ID=1 ",$count);
	   $result=M()->execute($sql);
		if ($result) {
			return $this->ajaxReturn ( true );
		} else {
			return $this->ajaxReturn ( false );
		}
		
	}
      /*2014-4-9 吕春城
	 * 设置首页内容
	 * **/
	public function setInstruction() {
        $islogin=session("isLogin");
        if(!$islogin){
            $this->ajaxReturn("您还没有登陆");
        }

		$content=I("content",0,'');

	   $sql=sprintf("update vote_message set  message_content='%s' where message_ID=1 ",$content);
	   $result=M()->execute($sql);
		if ($result) {
			return $this->ajaxReturn ( true );
		} else {
			return $this->ajaxReturn ( false );
		}
	}
	/*2014-4-9 吕春城
	 * 清空数据库评分数据和限制
	 * **/
	public function ClearData() {
        $islogin=session("isLogin");
        if(!$islogin){
            $this->ajaxReturn("您还没有登陆");
        }
    $sql="update vote_item set item_score=0,item_num=0,item_score1=0,item_score2=0,item_score3=0,item_score4=0,item_score5=0,item_score6=0,item_score7=0,item_score8=0,item_score9=0,item_score10=0,item_num1=0,item_num2=0,item_num3=0,item_num4=0,item_num5=0,item_num6=0,item_num7=0,item_num8=0,item_num9=0,item_num10=0";
		$result1=M()->execute($sql);
        $sql="delete from  vote_log";
        $result2=M('limit')->execute($sql);
        $sql="alter table  vote_log AUTO_INCREMENT 1" ;
        $result2=M('limit')->execute($sql);
    $sql="delete from  vote_limit";
     $result2=M('limit')->execute($sql);


    $myresult= array(
      'scoreeffect'=>$result1,
      'limitinit'=>$result2
     );
     return  $this->ajaxReturn($myresult);
	}
	/**更新密码*/
	public  function  UpdatePass()
	{
		$pass0=I("pass0","0","");
		$pass1=I("pass1","0","");
		$pass2=I("pass2","0","");
		$sql=sprintf("select count(*) from vote_admin where password='%s'",md5($pass0));
		$result=M()->query($sql);
		if($result[0]['count(*)']>0)
		{
			if($pass1==$pass2){
			$sql=sprintf("update vote_admin set password='%s' where user='admin'", md5($pass1));
			$result=M()->execute($sql);
			if($result>0){$this->ajaxReturn('修改成功');}
			else{$this->ajaxReturn("修改了$result个密码");}
			}
			else{
				$this->ajaxReturn('两次密码输入不一致');
			}
		}
		else{
			$this->ajaxReturn('原密码输入错误');
		}
		
	}
	/**更新类名称*/
  public function  UpdateClass()
  {
      $islogin=session("isLogin");
      if(!$islogin){
          $this->ajaxReturn("您还没有登陆");
      }
  	$content=I('content');
  	if($content){
  		$content=substr($content, 1);
  		$classArr=explode(',', $content);
  		 $count=0;
  		foreach ($classArr as $key=>$value)
  		{
  			$value=explode('|', $value);
  			$sql="update vote_class set class_name='$value[0]' where class_ID=$value[1]";
  		
  		    $result=   M()->execute($sql);
  		    $count+=$result;
  		}
  			$this->ajaxReturn("成功修改".$count."条记录");
  	}
  	else{
  	$this->ajaxReturn("获取数据失败，请使用chrome浏览器或360高速浏览器后再试");
  	}
  	
  }
  /*更新条目名称*/
  public function   UpdateItem()
  {
      $islogin=session("isLogin");
      if(!$islogin){
          $this->ajaxReturn("您还没有登陆");
      }
  		$content=I('content');
  	if($content){
  		$content=substr($content, 1);
  		$classArr=explode(',', $content);
  		 $count=0;
  		foreach ($classArr as $key=>$value)
  		{
  			$value=explode('|', $value);
  			$sql="update vote_item set item_name='$value[0]' where item_ID=$value[1]";
  		
  		    $result=   M()->execute($sql);
  		    $count+=$result;
  		}
  			$this->ajaxReturn("成功修改".$count."条记录");
  	}
  	else{
  	$this->ajaxReturn("获取数据失败，请使用chrome浏览器或360高速浏览器后再试");
  	}
  }
  /*更新评价项名称*/
  public function  UpdateScore()
  {
      $islogin=session("isLogin");
      if(!$islogin){
          $this->ajaxReturn("您还没有登陆");
      }
     $content=I('content');
  	if($content){
  		$content=substr($content, 1);
  		$classArr=explode(',', $content);
  		 $count=0;
  		foreach ($classArr as $key=>$value)
  		{
  			$value=explode('|', $value);
  			$sql="update vote_score set score_name='$value[0]' where score_ID=$value[1]";
  		
  		    $result=   M()->execute($sql);
  		    $count+=$result;
  		}
  			$this->ajaxReturn("成功修改".$count."条记录");
  	}
  	else{
  	$this->ajaxReturn("获取数据失败，请使用chrome浏览器或360高速浏览器后再试");
  	}
  }
  
}